# 当前项目名字，需要修改！
PROJECT_NAME='CommonKit'

# 编译工程
BINARY_NAME="${PROJECT_NAME}"

BUILD_SWIFT=true

cd FrameworkBuild/CommonKit

INSTALL_DIR=$PWD/../../Pod/Products
rm -fr "${INSTALL_DIR}"
mkdir $INSTALL_DIR
WRK_DIR=build

BUILD_PATH=${WRK_DIR}

DEVICE_DIR=${BUILD_PATH}/Release-iphoneos/${BINARY_NAME}.framework
SIMULATOR_DIR=${BUILD_PATH}/Release-iphonesimulator/${BINARY_NAME}.framework
RE_OS="Release-iphoneos"
RE_SIMULATOR="Release-iphonesimulator"

#xcodebuild -configuration "Release" MACH-O_TYPE='Static Library' -project "${PROJECT_NAME}.xcodeproj" -scheme "${BINARY_NAME}" -sdk iphoneos clean build CONFIGURATION_BUILD_DIR="${WRK_DIR}/${RE_OS}" LIBRARY_SEARCH_PATHS="./Pods/build/${RE_OS}" \
#
#xcodebuild ARCHS=x86_64 ONLY_ACTIVE_ARCH=NO -configuration "Release" MACH-O_TYPE='Static Library' -project "${PROJECT_NAME}.xcodeproj" -scheme "${BINARY_NAME}" -sdk iphonesimulator clean build CONFIGURATION_BUILD_DIR="${WRK_DIR}/${RE_SIMULATOR}" LIBRARY_SEARCH_PATHS="./Pods/build/${RE_SIMULATOR}" \
#    BUILD_LIBRARY_FOR_DISTRIBUTION=YES | xcpretty
    
xcodebuild -project "${PROJECT_NAME}.xcodeproj" \
           -scheme "${BINARY_NAME}" \
           CONFIGURATION_BUILD_DIR="${WRK_DIR}/${RE_SIMULATOR}" \
           LIBRARY_SEARCH_PATHS="./Pods/build/${RE_SIMULATOR}" \
           -sdk iphonesimulator clean build
xcodebuild -project "${PROJECT_NAME}.xcodeproj" \
           -scheme "${BINARY_NAME}" \
           CONFIGURATION_BUILD_DIR="${WRK_DIR}/${RE_OS}" \
           LIBRARY_SEARCH_PATHS="./Pods/build/${RE_OS}" \
           -sdk iphoneos clean build

echo " It is a test======="
echo "${DEVICE_DIR}"
echo "${SIMULATOR_DIR}"


if [ -d "${INSTALL_DIR}" ]
then
rm -rf "${INSTALL_DIR}"
fi
mkdir -p "${INSTALL_DIR}"


cp -rp "${DEVICE_DIR}" "${INSTALL_DIR}/"

INSTALL_LIB_DIR=${INSTALL_DIR}
mkdir -p "${INSTALL_LIB_DIR}"

lipo -create "${DEVICE_DIR}/${BINARY_NAME}" "${SIMULATOR_DIR}/${BINARY_NAME}" -output "${INSTALL_DIR}/${BINARY_NAME}.framework/${BINARY_NAME}"
if [ $BUILD_SWIFT = true ]
then
cp -rp ${SIMULATOR_DIR}/Modules/${BINARY_NAME}.swiftmodule/*.{swiftmodule,swiftdoc,swiftinterface} "$INSTALL_DIR/$BINARY_NAME.framework/Modules/$BINARY_NAME.swiftmodule"

cp -rp ${SIMULATOR_DIR}/Modules/${BINARY_NAME}.swiftmodule/Project/*.swiftsourceinfo "$INSTALL_DIR/$BINARY_NAME.framework/Modules/$BINARY_NAME.swiftmodule/Project"

else
　　echo"NO Swift file for build"
fi

#mkdir $PWD
#cd ../../


fileSwift="../../Pod/Products/${PROJECT_NAME}.framework/Headers/${PROJECT_NAME}-Swift.h"

echo "file-Swift.h==============$fileSwift"

#在 项目名.framework/Headers/项目名-Swift.h里面修改内容内容
#str1="#if 0"
#str2="#elif defined(__arm64__) && __arm64__"
#str3="#if defined(__x86_64__) && __x86_64__ || (__arm64__) && __arm64__"
##修改
##1.查找#if 0替换成空
#sed -i '' 's/#if 0//g' $fileSwift
##2.查找#elif defined(__arm64__) && __arm64__替换成空
#sed -i '' 's/#elif defined(__arm64__) && __arm64__//g' $fileSwift
##3.在1第一行添加字符串#if defined( __x86_64__ ) && __x86_64__ || (__arm64__) && __arm64__
#sed -i '' "1 a\\
#$str3" $fileSwift


rm -r "${WRK_DIR}"
